<?php

!defined('IN_VIFRAME') && die('Access Denied.');

Vi::import('Exception : Cms.Core.Cms');

class CCms extends TClass {

	protected
		$config = array();
	
	public function init() {
		$this->_initCoreConfigs();
		$this->_initIo();
		$this->_initDb();
		$this->_initOptions();
		$this->_initUser();
	}
	
	protected function _initCoreConfigs() {
		if (!Config::load('Config.Core', array())) {
			throw new ECms('Core.Config');
		}
		Config::load('Config.OptionId');
	}
	
	protected function _initIo() {
		$superGlobals = Config::get('Cms.Io:SuperGlobals');
		foreach ($GLOBALS as $Key => $Value) {
			if (!isset($superGlobals[$Key])) {
				$GLOBALS[$Key] = null;
				unset($GLOBALS[$Key]);
			}
		}
		
		if (isset($_GET['GLOBALS']) || isset($_POST['GLOBALS']) || isset($_COOKIE['GLOBALS'])
				|| isset($_FILES['GLOBALS'])) {
			
			throw new ECms('Core.Input.Taint');
		}
		
		if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
			$_GET    = Php::stripLashes($_GET);
			$_POST   = Php::stripLashes($_POST);
			$_COOKIE = Php::stripLashes($_POST);
			$_FILES  = Php::stripLashes($_FILES);
		}
		
		header('Content-Type: text/html; charset='.Config::get('Cms.Io:Charset'));
	}
	
	protected function _initDb() {
		Db::connect();
	}

	protected function _initOptions() {
		$Option = Factory::getInstance('Option');
		
		$Option->load('Site'         , 'Site');
		$Option->load('Site.Column'  , 'Site.Column');
		$Option->load('Site.Theme'   , 'Site.Theme');
		$Option->load('Site.Group'   , 'Site.Group');
		
		$Option->simpleLoad('Site.Visitors', 'Site.Visitors');
	}
	
	protected function _initUser() {
		$Option = Factory::getInstance('Option');
		
		$Option->set('User:Theme:Id', 0);
		$Option->set('User:Theme:Path', $Option['Theme'][0]['path']);
		
		$Option->set('User:UserId', 10000);
		$Option->set('User:Password', 'e10adc3949ba59abbe56e057f20f883e');
		
		if (true) {
			$result = Db::select('User', 'UserName,Signature,Avatar,SmsCount,ArticleCount,RecomArticleCount,Group', array(
				'UserId'   => $Option['User']['UserId'],
				'Password' => $Option['User']['Password']
			), array('GetArray' => true));
			if ($result) {
				$result[0] += $Option->get('User');
				$Option['User'] = $result[0];
			} else {
				//
			}
		} else {
			//
		}
	}
}